package com.ruoyi.web.controller.xcx;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.WeChatUtils;
import com.ruoyi.system.domain.AOrder;
import com.ruoyi.system.service.PayService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/xcx/pay")
public class PayController {

    @Autowired
    private PayService payService;

    /**
     * 支付转让出租数据
     * @param aOrder
     * @return
     */
    @RepeatSubmit
    @PostMapping("/walletPay")
    public AjaxResult walletPay(@RequestBody AOrder aOrder) {
        return AjaxResult.success(payService.walletPay(aOrder));
    }

    @PostMapping("/AOrderPayCallBack")
    public String AOrderPayCallBack(HttpServletRequest request, HttpServletResponse response) throws Exception{
        WxPayService miniWxPayService = WeChatUtils.getWxPayService(); //配置信息
        String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        WxPayOrderNotifyResult wxPayOrderNotifyResult = miniWxPayService.parseOrderNotifyResult(xmlResult);
        payService.wechatPayCallBack(wxPayOrderNotifyResult.getOutTradeNo(),wxPayOrderNotifyResult.getTransactionId());
        return WxPayNotifyResponse.success("处理成功!");
    }


    /**
     * 支付H5
     * @param aOrder
     * @return
     */
    @RepeatSubmit
    @PostMapping("/walletPayH")
    public AjaxResult walletPayH(@RequestBody AOrder aOrder) {
        return AjaxResult.success(payService.walletPayH(aOrder));
    }

    @PostMapping("/AOrderPayCallBackH")
    public String AOrderPayCallBackH(HttpServletRequest request, HttpServletResponse response) throws Exception{
        WxPayService miniWxPayService = WeChatUtils.getWxPayService(); //配置信息
        String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        WxPayOrderNotifyResult wxPayOrderNotifyResult = miniWxPayService.parseOrderNotifyResult(xmlResult);
        payService.wechatPayCallBackH(wxPayOrderNotifyResult.getOutTradeNo(),wxPayOrderNotifyResult.getTransactionId());
        return WxPayNotifyResponse.success("处理成功!");
    }

    @RepeatSubmit
    @PostMapping("/walletPayNa")
    public AjaxResult walletPayNa(@RequestBody AOrder aOrder) {
        return AjaxResult.success(payService.walletPayNa(aOrder));
    }

    @PostMapping("/AOrderPayNaCallBack")
    public String AOrderPayNaCallBack(HttpServletRequest request, HttpServletResponse response) throws Exception{
        WxPayService miniWxPayService = WeChatUtils.getWxPayService(); //配置信息
        String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        WxPayOrderNotifyResult wxPayOrderNotifyResult = miniWxPayService.parseOrderNotifyResult(xmlResult);
        payService.wechatPayCallBackNa(wxPayOrderNotifyResult.getOutTradeNo(),wxPayOrderNotifyResult.getTransactionId());
        return WxPayNotifyResponse.success("处理成功!");
    }

    /**
     * 用户变成vip
     * @param aOrder
     * @return
     */

    @RepeatSubmit
    @PostMapping("/walletPayUser")
    public AjaxResult walletPayUser(@RequestBody AOrder aOrder) {
        return AjaxResult.success(payService.walletPayUser(aOrder));
    }

    /**
     * 用户变成vip 回调
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @PostMapping("/AOrderPayUserCallBack")
    public String AOrderPayUserCallBack(HttpServletRequest request, HttpServletResponse response) throws Exception{
        WxPayService miniWxPayService = WeChatUtils.getWxPayService(); //配置信息
        String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        WxPayOrderNotifyResult wxPayOrderNotifyResult = miniWxPayService.parseOrderNotifyResult(xmlResult);
        payService.wechatPayCallBackUser(wxPayOrderNotifyResult.getOutTradeNo(),wxPayOrderNotifyResult.getTransactionId());
        return WxPayNotifyResponse.success("处理成功!");
    }

}
